package com.gateway.chris.filter;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

@Component
public class MyGatewayFilter implements GlobalFilter {

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        ServerHttpRequest request = exchange.getRequest();
        String token = request.getHeaders().getFirst("X-token");
        if (("/api/login").equals(request.getURI().getPath())) {
            // 登录接口 通过
            return chain.filter(exchange);
        }
        // 如果请求头不存在 Token
        if (token == null) {
            throw new RuntimeException("没登录");
        }
        return chain.filter(exchange);
    }
}
